<template>
  <div>
    <el-card>
      <el-form
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
        label-position="right"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="名称">
              <el-input
                v-model="searchForm.name"
                placeholder="请输入名称"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="text-align: center">
          <el-col :span="24">
            <el-form-item>
              <el-button @click="onreset">重置</el-button>
              <el-button type="primary" @click="onSearch"
                ><el-icon><Search /></el-icon>搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 表格部分 -->
    <el-card style="margin-top: 10px">
      <div style="margin-bottom: 5px">
        <el-button type="primary" plain size="small" @click="onAdd"
          ><el-icon><DocumentAdd /></el-icon>添加</el-button
        >
        <el-button
          type="danger"
          plain
          size="small"
          :disabled="multiple"
          @click="onDelete"
          ><el-icon><DeleteFilled /></el-icon>删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="100" />
        <el-table-column prop="term" label="名称" />
        <el-table-column prop="is_disable" label="是否禁用">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.is_disable === 0">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作选项">
          <template #default="{ row }">
            <el-button
              link
              plain
              type="primary"
              size="small"
              @click="onUpdate(row)"
              ><el-icon><View /></el-icon>修改</el-button
            >
            <el-button
              link
              plain
              type="primary"
              size="small"
              @click="onRevocation(row)"
              ><el-icon><Delete /></el-icon>删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 弹窗部分 -->
    <el-dialog
      v-model="addDialogVisible"
      :title="title"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="onReset(addRuleFormRef)"
      width="30%"
    >
      <el-form
        ref="addRuleFormRef"
        style="max-width: 450px"
        :model="addRuleForm"
        :rules="addRules"
        label-width="80"
        class="demo-ruleForm"
        status-icon
        @submit.prevent
      >
        <el-form-item
          label="名称"
          prop="name"
          name="name"
          required
          style="width: 300px"
        >
          <el-input
            v-model="addRuleForm.name"
            placeholder="请输入名称"
            clearable
          />
        </el-form-item>
        <el-form-item
          label="是否禁用"
          prop="is_disabled"
          name="is_disabled"
          style="width: 300px"
        >
          <el-switch v-model="addRuleForm.is_disabled" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="onReset(addRuleFormRef)" v-if="!imageUrl"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="onSubmit(addRuleFormRef)"
            v-if="!imageUrl"
            >提 交</el-button
          >
          <el-button
            type="primary"
            @click="onReset(addRuleFormRef)"
            v-if="imageUrl"
            >关 闭</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script  setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import {
  getSebsutuveList,
  updateSebsutuve,
  addSebsutuve,
  deleteSebsutuve
} from '@/api/sensitive/index.js'

const addDialogVisible = ref(false)
const addRuleFormRef = ref()
const title = ref('添加')
const total = ref(0)
const multiple = ref(true)
const ids = ref([])
const imageUrl = ref('')
// 添加表单的内容
const addRuleForm = ref({
  name: '',
  is_disabled: null
})
const updateId = ref()
const queryParams = ref({
  pageNo: 1,
  pageSize: 10
})
const searchForm = ref({
  name: ''
})
const tableData = ref([])
// 校验规则
const addRules = reactive({
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
})

onMounted(() => {
  getList()
})

const getList = async () => {
  const res = await getSebsutuveList({
    ...queryParams.value,
    term: searchForm.value.name
  })
  tableData.value = res.data.list.list
  total.value = res.data.list.total
}
// 增加
const onAdd = () => {
  title.value = '添加'
  addDialogVisible.value = true
}
// 删除一条
const onRevocation = (row) => {
  ElMessageBox.confirm('您确定要删除这条数据吗？', '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const ids = [row.id]
      const res = await deleteSebsutuve({ ids })
      ElMessage.success(res.message)
      getList()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除'
      })
    })
}
// 删除多条
const onDelete = () => {
  deleteSebsutuve({ ids: ids.value }).then((res) => {
    ElMessage.success(res.message)
    getList()
  })
}
// 表格多选
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id)
  multiple.value = !selection.length
}

// 搜索
const onSearch = () => {
  getList()
}
// 重置
const onreset = () => {
  searchForm.value = { id: '', name: '' }
  getList()
}
// 提交
const onSubmit = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (title.value === '修改') {
        updateSebsutuve({
          id: updateId.value,
          term: addRuleForm.value.name,
          is_disable: addRuleForm.value.is_disabled ? 0 : 1
        }).then((res) => {
          ElMessage({
            type: 'success',
            message: res.message
          })
          getList()
          addDialogVisible.value = false
        })
      } else {
        addSebsutuve({
          term: addRuleForm.value.name,
          is_disable: addRuleForm.value.is_disabled ? 0 : 1
        }).then((res) => {
          ElMessage({
            type: 'success',
            message: res.message
          })
          getList()
          addDialogVisible.value = false
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
// 重置
const onReset = (formEl) => {
  addRuleForm.value = {
    name: '',
    username: '',
    fileList: []
  }
  formEl.resetFields()
  addDialogVisible.value = false
  imageUrl.value = ''
}

// 修改
const onUpdate = async (row) => {
  title.value = '修改'
  updateId.value = row.id
  addRuleForm.value.name = row.term
  addRuleForm.value.is_disabled = row.is_disable === 0
  addDialogVisible.value = true
}
</script>

<style lang="scss" scoped>
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
:deep(.hide-upload-btn .el-upload--picture-card) {
  display: none;
}
:deep(.el-table .el-table__header-wrapper th) {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px !important;
  font-size: 13px;
}

:deep(.el-form .el-form-item .el-form-item__label) {
  font-weight: bold !important;
}

:deep(.el-form .el-form-item .second_label .el-form-item__label) {
  font-weight: bold !important;
}
:deep(.second_label .el-form-item__error) {
  position: static !important;
}
.buttons {
  text-align: center;
}
</style>
